package com.atguigu.dga230315.governance.assess.spec;

import com.atguigu.dga230315.governance.assess.Assessor;
import com.atguigu.dga230315.governance.bean.AssessParam;
import com.atguigu.dga230315.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga230315.meta.bean.TableMetaInfo;
import com.atguigu.dga230315.meta.constant.MetaConst;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component("TABLE_NAME_STANDARD")
public class TableNameStandardAssess extends Assessor {


    Pattern odsPattern = Pattern.compile("^ods_.+_(inc|full)$");
    Pattern dwdPattern = Pattern.compile("^dwd_.+_.+_(inc|full)$");
    Pattern dimPattern = Pattern.compile("^dim_.+_(zip|full)$");
    //  \d代表的是任意数字 + 代表的是匹配一次或多次
    //如果要按照上课学习的离线命名可以写为^dws_.+_.+_.+_(1d|nd|td)$
    //如果是指明表中存的是几天前的数据 那么就用现在代码里的这个匹配方式
    Pattern dwsPattern = Pattern.compile("^dws_.+_.+_.+_(\\d+d|td)$");
    Pattern adsPattern = Pattern.compile("^ads_.+");
    Pattern dmPattern = Pattern.compile("^dm_.+");

    @Override
    protected void assess(GovernanceAssessDetail assessDetail, AssessParam assessParam) throws ParseException {
        //判断表名是否合规
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        //思路：1.先获取表名
        String tableName = tableMetaInfo.getTableName();

        //2.直接再获取层级
        String dwLevel = tableMetaInfo.getTableMetaInfoExtra().getDwLevel();

        //5.对应other层的表直接给5分
        if (dwLevel.equals(MetaConst.DW_LEVEL_OTHER)){
            assessDetail.setAssessScore(BigDecimal.valueOf(5));
            assessDetail.setAssessProblem("未纳入分层");
            String url = assessParam.getGovernanceMetric().getGovernanceUrl() + tableMetaInfo.getId();
            assessDetail.setGovernanceUrl(url);
            return;
        }

        //3.根据层级获取到对应的正则表达式
        Pattern pattern = null;
        switch (dwLevel) {
            case "ODS":
                pattern = odsPattern;
                break;
            case "DWD":
                pattern = dwdPattern;
                break;
            case "DWS":
                pattern = dwsPattern;
                break;
            case "DIM":
                pattern = dimPattern;
                break;
            case "DM":
                pattern = dmPattern;
                break;
            case "ADS":
                pattern = adsPattern;
                break;
        }

        //4.匹配（匹配的是同层表）
        Matcher matcher = pattern.matcher(tableName);
        if (!matcher.matches()){  //获取匹配不上的
            assessDetail.setAssessScore(BigDecimal.ZERO);
            assessDetail.setAssessProblem("表名不合规范");
            assessDetail.setAssessComment(pattern.toString());
        }



    }
}
